package com.web44.ssm.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.web44.ssm.bean.Book;
import com.web44.ssm.bean.QueryVo;
import com.web44.ssm.service.IBookSevice;
import com.web44.ssm.service.impl.BookSevice;
import com.web44.ssm.utils.ResponseVo;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Title: BookController
 * @Description:
 * @Auther: py
 * @Version: 1.0
 * @create 2024/1/15 13:43
 */
@RestController
@RequestMapping("book")
public class BookController {

    @Autowired
    private IBookSevice bookSevice;



    @PostMapping("save")
    public ResponseVo saveBookWeb(@RequestBody  Book book) {
        if(false) {
            bookSevice.saveBook(book);
        }else {
            throw  new RuntimeException("保存失败，系统异常");
        }

        return ResponseVo.ok("保存成功!");
    }

    @PutMapping ("edit")
    public ResponseVo editBook(@RequestBody Book book) {
        bookSevice.editBook(book);
        return ResponseVo.ok("修改成功!");
    }

    // delete/1
    @DeleteMapping("delete/{id}")
    public ResponseVo deleteBookById(@PathVariable("id") Integer id2) {
         bookSevice.deleteBookById(id2);
         return ResponseVo.ok("删除成功!");
    }


    @GetMapping("query")
    public ResponseVo queryBookById(Integer id) {
         Book book =  bookSevice.queryBookById(id);
        return ResponseVo.ok("检索成功!",book);
    }

    @GetMapping("queryAll")
    public ResponseVo queryBooks() {
        List<Book> books =  bookSevice.queryBooks();
        return ResponseVo.ok("检索成功!",books);
    }

    @GetMapping("queryPage")
    public  ResponseVo queryBooksByPage(QueryVo queryVo) {
        System.out.println(1/0);
        PageInfo<Book> page = bookSevice.queryBooksByPage(queryVo);
        System.out.println("正在检索书籍....");
        return ResponseVo.ok("检索成功!",page);
    }
}
